#pragma once
#include <iostream>
#include <unistd.h>
#include <string>
#include <fstream>
#include <unordered_map>
#include "Log.hpp"
using namespace log_ns;
const static std::string sep=": ";
class Dict
{
private:
    void LoadDict(const std::string &path)
    {
        std::ifstream in(path);
        if (!in.is_open())
        {
           LOG(FATAL,"open is fail!!\n",path.c_str());
            exit(1);
        }
        std::string line;
        while(std::getline(in,line)) //接收一段话
        {
            LOG(DEBUG,"Load info:%s, success\n",line.c_str());
            if(line.empty()) //判空
            {
                continue;
            }
            auto pos=line.find(sep); //匹配到对应的符号
            if(pos==std::string::npos)
            {
                continue;
            }
            std::string key=line.substr(0,pos); //截取到从0开始的到pos位置的字符
            if(key.empty()) //单词的前半部分判空
            {
                continue;
            }
            std::string value=line.substr(pos+sep.size()); //截取到从pos+sep.size()位置往后的字符截取下来
            if(value.empty())
            {
                continue;
            }
            _dict.insert(std::make_pair(key,value)); //插入键值对

          
        }
        LOG(INFO,"load %s done\n",path.c_str());
        in.close();
    }
public:
    Dict(const std::string &dict_path) : _dict_path(dict_path)
    {
        LoadDict(_dict_path);
    }
    std::string Translate(const std::string word)//查找字典中是否有对应的的单词
    {
        if(word.empty()) return "None";
        auto iter=_dict.find(word);
        if(iter==_dict.end()) return "None";  //迭代器不能找到对应单词
        return iter->second;//如果找到单词，iter 指向对应的键值对
    }
    ~Dict()
    {
         
    }

private:
    std::unordered_map<std::string, std::string> _dict; // std::unordered_map 是一种关联容器，用于存储键值对（key - value pairs）。
    std::string _dict_path;
};